package com.tools.service.impl;

import com.tools.commons.utils.BeanUtils;
import com.tools.commons.utils.IdGenerator;
import com.tools.model.entity.TasksEntity;
import com.tools.model.params.TasksParams;
import com.tools.repository.TaskRepository;
import com.tools.security.context.SecurityContextHolder;
import com.tools.service.TaskService;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TaskServiceImp implements TaskService {
    private final TaskRepository taskRepository;

    @Override
    public void saveTask(TasksParams tasksParams) {
        TasksEntity tasksEntity = new TasksEntity();
        BeanUtils.updateProperties(tasksParams, tasksEntity);
        tasksEntity.setUserId(SecurityContextHolder.getContext().getUserInfo().getUserId());
        tasksEntity.setTaskId(IdGenerator.uuid());
        taskRepository.save(tasksEntity);
    }

    @Override
    public void deleteTask(String taskId) {
        taskRepository.deleteByTaskId(taskId);
    }

    @Override
    public TasksEntity findByTaskid(String taskId) {
        return taskRepository.findByTaskId(taskId);
    }


    @Override
    public List<TasksEntity> findTaskListByUserId(String userid) {
        return taskRepository.findByUserId(userid);
    }



}
